import { ref, onMounted, onUnmounted } from 'vue';

interface TableHeightOptions {
  /** 容器元素的ref引用 */
  containerRef: any;
  /** 额外的高度偏移量，默认为82 */
  offset?: number;
}

/**
 * 表格高度自适应Hook
 * @param options 配置选项
 * @returns 表格高度响应式引用
 */
export function useTableHeight(options: TableHeightOptions) {
  const { containerRef, offset = 82 } = options;
  const tableHeight = ref(0);

  const updateTableHeight = () => {
    if (containerRef.value) {
      const containerRect = containerRef.value.getBoundingClientRect();
      const tableTop = containerRect.top;
      tableHeight.value = window.innerHeight - tableTop - offset;
    }
  };

  const handleResize = () => {
    updateTableHeight();
  };

  onMounted(() => {
    updateTableHeight();
    window.addEventListener('resize', handleResize);
  });

  onUnmounted(() => {
    window.removeEventListener('resize', handleResize);
  });

  return {
    tableHeight
  };
}
